/**

 @Name：layuiAdmin 公共业务
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */
 
layui.define(function(exports){
  var $ = layui.$
  ,layer = layui.layer
  ,laytpl = layui.laytpl
  ,setter = layui.setter
  ,view = layui.view
  ,admin = layui.admin

  
  //公共业务的逻辑处理可以写在此处，切换任何页面都会执行
  //……
    admin.seatData = function () {
        var cacheData = layui.data(layui.setter.tableName);
        var user = cacheData[layui.setter.userKey];
        $("#seatStatus").val(user.seatStatus)
        $("#channelId").val(user.channelId)
        $("#channelPwd").val(user.channelPwd)
        $("#seatHost").val(user.seatHost)
        $("#seatPort").val(user.seatPort)
        $("#seatType").val(user.seatType);
    }

    admin.error = function (e) {
        var cacheData = layui.data(layui.setter.tableName);
        var token = cacheData[layui.setter.request.tokenName];
        if(!token){
            layer.alert('会话已失效，请重新登录',{icon: 7,
                yes:function (index) {
                    admin.events.logout();
                }
            });
        }else{
            var rtn = admin.syncReq(ctx+'/system/login/isLogin',{});
            if(!rtn.success){
                layer.alert('会话已失效，请重新登录',{icon: 7,
                    yes:function (index) {
                        admin.events.logout();
                    }
                });
            }
        }

    };

    //同步请求 //cjianquan 2020/2/7
    admin.syncReq = function (url,data,options) {
        if(!options){
            options = {};
        }
        if(!options.error){
            options.error=admin.error;
        }
        options = $.extend(options,{url:url,type:'post',async:false,data:data,done:function (response) {
            rtn = response;
        }});
        var rtn = null;
        view.req(options);
        return rtn;
    };

  //异步请求 //cjianquan 2020/2/7
    admin.asyncReq = function (url,data,callbackFn) {
        view.req({url:url,type:'post',data:data,done:callbackFn});
    };
  
  
  
  //退出
  admin.events.logout = function(){
    //执行退出接口
    admin.req({
      url: ctx + '/system/login/logout'
      ,type: 'get'
      ,data: {}
      ,done: function(res){ //这里要说明一下：done 是只有 response 的 code 正常才会执行。而 succese 则是只要 http 为 200 就会执行
        //清空本地记录的 token，并跳转到登入页
        admin.exit(function(){
            try{
                parent.location.href = ctxLayui+'/views/user/login.jsp';
            }catch(e){
                location.href = ctxLayui+'/views/user/login.jsp';
            }

        });
      }
    });
  };

  
  //对外暴露的接口
  exports('common', {});
});